/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.info;

import java.math.BigInteger;
import java.security.SecureRandom;

import nu.korkad.daytracker.R;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class AppInfo
{
    /**
     * Get version code and return it.
     * 
     * @param context
     * @return int
     */
    public static int getVersionCode(Context context)
    {
        try
        {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA).versionCode;
        }
        catch (NameNotFoundException e)
        {
            return 0;
        }
    }
    
    /**
     * Get version name and return it.
     * 
     * @param context
     * @return String
     */
    public static String getVersionName(Context context)
    {
        try
        {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA).versionName;
        }
        catch (NameNotFoundException e)
        {
            return context.getString(R.string.Unknown);
        }
    }
    
    /**
     * Generate device id and return it.
     * 
     * @param context
     * @return String
     */
    public static String generateDeviceId(Context context)
    {
        SecureRandom random = new SecureRandom();
        return new BigInteger(20, random).toString(32);
    }
}
